package com.go.where.manager.action;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.go.where.manager.common.Constants;
import com.go.where.manager.pojo.user.Menu;
import com.go.where.manager.pojo.user.UserInfoVO;
import com.go.where.manager.util.CommonUtil;
import com.google.gson.reflect.TypeToken;
import com.whalin.MemCached.MemCachedClient;

@SuppressWarnings("unchecked")
public class MainAction extends SuperAction {

	private static final long serialVersionUID = -1570442984220960806L;

	public String main() {
		return "main";
	}

	@Resource(name = "memcachedClient")
	private MemCachedClient memCachedClient;
	List<Menu> permissionList;
	private String serviceCode;
	private UserInfoVO operator;

	public UserInfoVO getOperator() {
		return operator;
	}

	public void setOperator(UserInfoVO operator) {
		this.operator = operator;
	}

	public String getServiceCode() {
		return serviceCode;
	}

	public void setServiceCode(String serviceCode) {
		this.serviceCode = serviceCode;
	}

	public List<Menu> getPermissionList() {
		return permissionList;
	}

	public void setPermissionList(List<Menu> permissionList) {
		this.permissionList = permissionList;
	}

	// 后台Header
	public String header() {
		operator = this.getCurrentOperator();
		String menu1 = Constants.MENU1;
		Object ob = memCachedClient.get(menu1);
		if (ob instanceof HashMap) {
			List<String> list = CommonUtil.gson().fromJson(
					this.getCurrentOperator().getServiceCode(),
					new TypeToken<List<String>>() {
					}.getType());
			Map<String, Menu> map = (HashMap<String, Menu>) ob;
			if (permissionList == null) {
				permissionList = new ArrayList<Menu>();
			}
			if (list != null) {
				for (String co : list) {
					Menu p = map.get(co);
					if (p != null)
						permissionList.add(p);
				}
			}
		}
		if (permissionList != null && permissionList.size() > 1) {
			Collections.sort(permissionList, new Comparator<Menu>() {
				@Override
				public int compare(Menu o1, Menu o2) {
					if ((o1.getMenuSort() != null) && o2.getMenuSort() != null) {
						int i = o1.getMenuSort() - o2.getMenuSort();
						if (i != 0) {
							return i;
						}
					}
					return Integer.parseInt(o1.getMenuId())
							- Integer.parseInt(o2.getMenuId());
				}
			});
		}
		return "header";
	}

	// 后台左边菜单
	public String menu() {
		List<String> codeList = CommonUtil.gson().fromJson(
				this.getCurrentOperator().getServiceCode(),
				new TypeToken<List<String>>() {
				}.getType());
		if (codeList.size() > 0) {
			List<Menu> ob = this.getMenuLv2(serviceCode, codeList);
			if (ob instanceof ArrayList) {
				List<Menu> retList = (ArrayList<Menu>) ob;
				for (Menu p : retList) {
					if (permissionList == null)
						permissionList = new ArrayList<Menu>();
					if (p != null)
						permissionList.add(p);
				}
			}
		}
		return "menu";
	}

	// 后台首页
	public String index() {
		return "index";
	}

	// 后台中间(显示/隐藏菜单)
	public String middle() {
		return "middle";
	}

	public List<Menu> getMenuLv2(String code, List<String> codeList) {
		List<Menu> list = new ArrayList<Menu>();
		Object ob = memCachedClient.get(Constants.MENU2);
		if (ob instanceof ArrayList) {
			ArrayList<Menu> list2 = (ArrayList<Menu>) ob;
			for (Menu p : list2) {
				if (p != null && p.getMenuParent() != null
						&& p.getMenuParent().equals(code)
						&& codeList.contains(p.getMenuId())) {
					p.setList(getMenuLv3(p.getMenuId(), codeList));
					list.add(p);
				}
			}
		}
		Collections.sort(list, new Comparator<Menu>() {
			@Override
			public int compare(Menu o1, Menu o2) {
				if (o1.getMenuSort() != null && o2.getMenuSort() != null) {
					int i = o1.getMenuSort() - o2.getMenuSort();
					if (i != 0) {
						return i;
					}
				}
				return Integer.parseInt(o1.getMenuId())
						- Integer.parseInt(o2.getMenuId());
			}
		});
		return list;
	}

	public <T> List<Menu> getMenuLv3(String code, List<String> codeList) {
		List<Menu> list = new ArrayList<Menu>();
		Object ob = memCachedClient.get(Constants.MENU3);
		if (ob instanceof ArrayList) {
			ArrayList<Menu> list3 = (ArrayList<Menu>) ob;
			for (Menu p : list3) {
				if (p != null && p.getMenuParent() != null
						&& p.getMenuParent().equals(code)
						&& codeList.contains(p.getMenuParent())) {
					list.add(p);
				}
			}
		}
		Collections.sort(list, new Comparator<Menu>() {
			@Override
			public int compare(Menu o1, Menu o2) {
				if (o1.getMenuSort() != null && o2.getMenuSort() != null) {
					int i = o1.getMenuSort() - o2.getMenuSort();
					if (i != 0) {
						return i;
					}
				}
				return Integer.parseInt(o1.getMenuParent())
						- Integer.parseInt(o2.getMenuParent());
			}
		});
		return list;
	}

}
